//
//  QYXRankingInformationViewController.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXRankingInformationViewController.h"
#import "QYXRankingInforCollectionCell.h"
#import "QYXTopRankingModel.h"
#import "QYXCommentViewController.h"//评论页
#import "VideoFullScreenController.h"//播放
#import "QYXTopRankingPlayInfoModel.h"
#import "AppDelegate.h"

@interface QYXRankingInformationViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, PushCommentPage>

@property (nonatomic, strong) UICollectionView *rankingInforCollectionView;

/** 箭头 */
@property (nonatomic, strong) UIImageView *topArrowImageView;
/** 返回 */

@property (nonatomic, strong) UILabel *eyeBackLabel;


@end

@implementation QYXRankingInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatRankingInforCollectionView];
}


#pragma  mark -- 创建主要集合视图:
- (void)creatRankingInforCollectionView

{
    
    
    //箭头分栏:
    
    self.topArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"subfield"]];
    UIView *arrowView = [[UIView alloc] initWithFrame:CGRectMake(38, 26, 16, 16)];
    [arrowView addSubview:_topArrowImageView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:arrowView];
    
    self.topArrowImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(arrowTuringAction)];
    [self.topArrowImageView addGestureRecognizer:tapGesture];
    
    
    //返回眼:
    
    UIView *eyeBackView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth - 40, 26, 30, 30)];
    self.eyeBackLabel = [[UILabel alloc] initWithFrame:eyeBackView.bounds];
    self.eyeBackLabel.text = @"QYX";
    self.eyeBackLabel.font = [UIFont fontWithName:@"Lobster 1.4" size:14];
    
    [eyeBackView addSubview:_eyeBackLabel];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:eyeBackView];
    
    self.eyeBackLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *eyeTapGesture = [[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(didEyeTurnBack)];
    [self.eyeBackLabel addGestureRecognizer:eyeTapGesture];
    
    
    
    
    //导航栏:
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth / 4, 64)];
    titleLabel.text = @"Eyepelizer";
    titleLabel.textAlignment = 1;
    titleLabel.font = [UIFont fontWithName:@"Lobster 1.4" size:25];
    self.navigationItem.titleView = titleLabel;
    
    //    //主要集合视图:
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:view];
    
    
    UICollectionViewFlowLayout *rankingInforFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    rankingInforFlowLayout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - 64);
    rankingInforFlowLayout.minimumLineSpacing = 0;
    rankingInforFlowLayout.minimumInteritemSpacing = 0;
    rankingInforFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    self.rankingInforCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) collectionViewLayout:rankingInforFlowLayout];
    self.rankingInforCollectionView.backgroundColor = [UIColor clearColor];
    self.rankingInforCollectionView.pagingEnabled = YES;
    self.rankingInforCollectionView.delegate = self;
    self.rankingInforCollectionView.dataSource = self;
    [self.view addSubview:_rankingInforCollectionView];
    
    //注册cell:
    
    [self.rankingInforCollectionView registerClass:[QYXRankingInforCollectionCell class] forCellWithReuseIdentifier:@"rankingInforCell"];
    
}


#pragma mark -- 分栏点击方法:

- (void)arrowTuringAction
{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.topArrowImageView.transform = CGAffineTransformRotate(self.topArrowImageView.transform, M_PI / 2);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark -- 返回眼点击方法:

- (void)didEyeTurnBack

{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5f;
    transition.delegate = self;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    transition.subtype = kCATransitionFromTop;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    AppDelegate *tempAppDe = [UIApplication sharedApplication].delegate;
    tempAppDe.tabBar.backView.hidden = NO;
    [self.navigationController popViewControllerAnimated:NO];
    
}


#pragma mark -- 签协议必须实现方法:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{
    
    return self.rankingInforDataSource.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{
    
    QYXTopRankingModel *model = [self.rankingInforDataSource objectAtIndex:indexPath.row];
    
    QYXRankingInforCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rankingInforCell" forIndexPath:indexPath];
    
    cell.delegate = self;
    cell.rankingInforModel = model;
    
    return cell;
    
}


- (void)viewDidLayoutSubviews

{
    if (_RankingSelectNum) {
        
        [self.rankingInforCollectionView setContentOffset:CGPointMake(kScreenWidth * _RankingSelectNum, 0) animated:NO];
        
    }
    
}


- (void)pushCommentPageWithId:(NSInteger)modelId

{
    
    QYXCommentViewController *commtentVC = [[QYXCommentViewController alloc] init];
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5f;
    transition.delegate = self;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromTop;
    commtentVC.commentId = modelId;
    commtentVC.tempDataArray = self.rankingInforDataSource;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController pushViewController:commtentVC animated:NO];
    
    
}

#pragma mark -- 播放视频页面

- (void)tapPlay:(QYXTopRankingModel *)model

{
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    for (QYXTopRankingPlayInfoModel *playModel in model.playInfo) {
        
        [dic setObject:playModel.url forKey:playModel.name];
        
    }
    
    VideoFullScreenController *videoVC = [[VideoFullScreenController alloc] initWithPlayUrl:((QYXTopRankingPlayInfoModel *)model.playInfo.firstObject).url AndPlaySort:dic];
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5f;
    transition.delegate = self;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    transition.subtype = kCATransitionFromTop;
    [self.view.layer addAnimation:transition forKey:nil];
    [self presentViewController:videoVC animated:YES completion:nil];
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
